/* INCLUDES ******************************************************************/

#include "ses_timer.h"
#include "ses_led.h"
#include "ses_common.h"
#include  <avr/io.h>
#include "ses_scheduler.h"

/*GLOABLE VARIALBEL**********************************************************/

/* DEFINES & MACROS **********************************************************/

#define TIMER2_INT_vect  			TIMER2_COMPA_vect	// Interrupt Vector for Timer2
#define TIMER2_MODE_A				0x02				// WGM21=1 WGM20=0, CTCmode
#define TIMER2_MODE_B				0x04				// WGM22=0, CS22=1,CS21,20=0; prescaler= 64;
#define TIMER2_INT_MASK				0x02				// OCIE2A=1
#define TIMER2_INT_FLAG         	0X02				// OCF2A=1
#define TIMER2_CYC_FOR_1MILLISEC	0xfa				// Top value;

/*FUNCTION DEFINITION ********************************************************/

pTimerCallback timercall;


/**
 * Initializes hardware timer2 of MCU.
 * Timer callback is executed each millisecond.
 *
 * @param cb valid pointer to callback function.
 */
void timer2_init(pTimerCallback cb) {
	TIMSK2= TIMER2_INT_MASK;
	TIFR2 = TIMER2_INT_FLAG;
	TCCR2A = TIMER2_MODE_A;
	TCCR2B = TIMER2_MODE_B;
	OCR2A = TIMER2_CYC_FOR_1MILLISEC;
	timercall=cb;
}


/*
 * Interrupt service routine for Timer2.
 * @sysms_increment function increments every single MS
 */
ISR(TIMER2_INT_vect) {
	sysms_increment();
	timercall();
	}
